﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseInterface;
using DatabaseInterface.DataBase;
using System.Data.SqlClient;

namespace BaseUnits.Builders
{
    public class GridBuilder
    {
        Connection connection;
        public GridBuilder(Connection connection)
        {
            this.connection = connection;
        }
        public List<IPowerGrid> GetGrids()
        {
            List<IPowerGrid> grids = new List<IPowerGrid>();
            String sqlCommandText;

            SqlConnection cnx = this.connection.getConnection();
            sqlCommandText = "SELECT GridId, Description, Country FROM Grids";
            SqlCommand cmd = new SqlCommand(sqlCommandText, cnx);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DatabaseInterface.DataBase.PowerGrids gridsTable = new DatabaseInterface.DataBase.PowerGrids();
            da.Fill(gridsTable.Grids);
            int gridId;
            String description;
            String country;
            Grid newGrid;

            DatabaseInterface.DataBase.PowerGrids.GridsRow row;
            for (int i = 0; i < gridsTable.Grids.Rows.Count; i++)
            {
                row = (PowerGrids.GridsRow)gridsTable.Grids.Rows[i];

                gridId = Convert.ToInt32(row["GridId"]);
                description = Convert.ToString(row["Description"]);
                country = Convert.ToString(row["Country"]);
                newGrid = new Grid(gridId, description, country);

                PowerPlantBuilder ppBuilder = new PowerPlantBuilder(this.connection);
                foreach (IPowerPlant p in ppBuilder.getPowerPlants(gridId.ToString()))
                {
                    newGrid.AddPowerPlant(p);
                }
                grids.Add(newGrid);
            }
            return grids;
        }
        public IPowerGrid GetGrid(int powerPlantId)
        {
            Grid newGrid;
            String sqlCommandText;
            SqlConnection cnx = this.connection.getConnection();
            sqlCommandText = "SELECT GridId, Description, Country FROM Grids WHERE GridId in (Select GridId FROM GridPlants WHERE PlantId = @PowerPlantId)";
            SqlCommand cmd = new SqlCommand(sqlCommandText, cnx);

            cmd.Parameters.Add("@PowerPlantId", System.Data.SqlDbType.Int);
            cmd.Parameters["@PowerPlantId"].Value = powerPlantId;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DatabaseInterface.DataBase.PowerGrids grids = new DatabaseInterface.DataBase.PowerGrids();
            da.Fill(grids.Grids);
            int gridId;
            String description;
            String country;

            DatabaseInterface.DataBase.PowerGrids.GridsRow row = (PowerGrids.GridsRow)grids.Grids.Rows[0];
            
            gridId = Convert.ToInt32(row["GridId"]);
            description = Convert.ToString(row["Description"]);
            country = Convert.ToString(row["Country"]);
            newGrid = new Grid(gridId, description, country);

            PowerPlantBuilder ppBuilder = new PowerPlantBuilder(this.connection);
            foreach (IPowerPlant p in ppBuilder.getPowerPlants(gridId.ToString()))
            {
                newGrid.AddPowerPlant(p);
            }

            return newGrid;
        }
    }
}
